/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#include "projecttreeplugin.h"
#include <QtGui/QDockWidget>
#include <QtGui/QMenuBar>
#include <QtPlugin>

using namespace Try;

ProjectTreePlugin::ProjectTreePlugin(QObject* parent)
    :   QObject(parent),
        m_projectTree(0)
{
}

ProjectTreePlugin::~ProjectTreePlugin()
{
    this->shutdown();
}

QString ProjectTreePlugin::name() const
{
    return tr("Project Tree");
}

bool ProjectTreePlugin::initialize(QMainWindow* parent)
{
    if (parent)
    {
        this->shutdown();

        // Widgets.
        m_projectTree = new ProjectTree(parent);

        QDockWidget* pt_dock_widget = new QDockWidget(parent);
        pt_dock_widget->setWidget(m_projectTree);
        pt_dock_widget->setWindowTitle(m_projectTree->windowTitle());
        parent->addDockWidget(Qt::RightDockWidgetArea, pt_dock_widget);

        /*QObject::connect(m_projectTree, SIGNAL(projectSelected()), this, SLOT(onSelectProject()));
        QObject::connect(m_projectTree, SIGNAL(objectSelected(Object*)), this, SLOT(onSelectObject(Object*)));
        QObject::connect(m_projectTree, SIGNAL(addObjectRequest()), this, SLOT(onAddObject()));
        QObject::connect(m_projectTree, SIGNAL(copyObjectRequest(Object*)), this, SLOT(onCopyObject(Object*)));
        QObject::connect(m_projectTree, SIGNAL(deleteObjectRequest(Object*)), this, SLOT(onDeleteObject(Object*)));
        QObject::connect(m_projectTree, SIGNAL(propertySelected(Property*)), this, SLOT(onSelectProperty(Property*)));
        QObject::connect(m_projectTree, SIGNAL(deletePropertyRequest(Property*)), this, SLOT(onDeleteProperty(Property*)));*/

        // Menus.
        QMenuBar* menu_bar = parent->menuBar();
        QMenu* view_menu = menu_bar->findChild<QMenu*>("menuView");

        QAction* project_tree_action = pt_dock_widget->toggleViewAction();
        project_tree_action->setText(tr("&Project Tree"));

        view_menu->insertAction(0, project_tree_action);

        // Connections.
        QObject::connect(m_projectTree, SIGNAL(destroyed()), pt_dock_widget, SLOT(deleteLater()));
        QObject::connect(m_projectTree, SIGNAL(destroyed()), project_tree_action, SLOT(deleteLater()));

        return true;
    }

    return false;
}

bool ProjectTreePlugin::shutdown()
{
    if (m_projectTree)
        delete m_projectTree;

    m_projectTree = 0;

    return true;
}

Q_EXPORT_PLUGIN2(projecttree, ProjectTreePlugin)
